<template>
  <div class="content">
    <div class="nav">
      <van-nav-bar :title="friendInfoData.nickName" left-text="返回" left-arrow />
    </div>
    <div id="chat">
      <div
        v-for="item in chat_message.data"
        :key="item.id"
        :class="chat_message.isFriendSend(item) ? 'friend_lists' : 'me_lists'"
      >
        <div class="header_img">
          <img
            :src="chat_message.isFriendSend(item) ? friendInfoData.faceImage : currentUserInfoData.faceImage"
          />
        </div>
        <div :class="'msg-wrapper ' +  (chat_message.isFriendSend(item) ? 'right' : 'left')">
          <p
            :class="chat_message.isFriendSend(item) ? 'msg-left-white' : 'msg-right-green'"
          >{{item.msg}}</p>
        </div>
      </div>
    </div>

    <div class="send">
      <div class="s">
        <van-search
          v-model="send_message.text"
          show-action
          placeholder="输入信息..."
          @search="onSearch"
        >
          <template #action>
            <van-button
              :type="send_message.text.length>0?'primary': 'default'"
              @click="send_message.click_send_btn"
            >发送</van-button>
          </template>
          <template #left-icon>
            <van-icon name="smile-o" />
          </template>
        </van-search>
      </div>
    </div>
  </div>
</template>


<script lang="ts">
import router from "@/router/index";
import { Notify } from "vant";
import { onMounted, reactive, ref } from "vue";
import {
  getFriendDataCache,
  getUserDataCache,
  getUserId,
  addChatDataCache,
} from "@/utils/app";
import { strategy, sendChatMessage, ChatMessage } from "@/service/WsService";
import { findMyChatMsgOfFriend } from "@/api/requests/base";

export default {
  setup() {
    const firendId: any = router.currentRoute.value.params.firendId;
    const friendInfoData = getFriendDataCache(firendId);
    if (firendId == null || friendInfoData == null) {
      Notify({ type: "danger", message: "未知错误" });
      setTimeout(() => {
        router.push("/");
      }, 1000);
      return;
    }
    const currentUserInfoData = getUserDataCache();
    const chat_message_data: ChatMessage[] = [];
    const chat_message = ref({
      data: chat_message_data,
      get: () => {
        findMyChatMsgOfFriend(firendId).then((res: any) => {
          chat_message.value.data = res.data.content;
          chat_message.value.toBottom();
        });
      },
      put: (msg: ChatMessage) => {
        chat_message.value.data.push(msg);
        chat_message.value.toBottom();
      },
      isFriendSend: (item: ChatMessage) => {
        return item.sendUserId == firendId;
      },
      toBottom: () => {
        const chatDOM = document.getElementById("chat");
        if (chatDOM != null) {
          setTimeout(() => {
            chatDOM.scrollTop =
              chatDOM.scrollHeight + chatDOM.offsetHeight + 200;
          }, 0);
        }
      },
    });
    window.onresize = () => {
      chat_message.value.toBottom();
    };

    chat_message.value.get();
    const send_message = ref({
      text: "",
      click_send_btn: () => {
        if (send_message.value.text.length == 0) {
          return;
        }
        const chatMessageObj = {
          sendUserId: currentUserInfoData.id,
          acceptUserId: firendId,
          msg: send_message.value.text,
        };
        sendChatMessage(chatMessageObj);
        chat_message.value.put(chatMessageObj);
        send_message.value.text = "";
      },
    });

    strategy.chat = (message: ChatMessage) => {
      console.log("aaa");

      console.log(message);
      console.log(firendId);

      if (message.acceptUserId == getUserId()) {
        // 如果是当前界面的好友发送的
        chat_message.value.put(message);
      } else {
        // 如果不是
      }
    };
    return {
      firendId,
      friendInfoData,
      currentUserInfoData,
      send_message,
      chat_message,
    };
  },
};
</script>



<style lang="less" scoped>
.content {
  height: 100vh;
  overflow: hidden;
  .nav {
    // position: absolute;
    // top: 0px;
  }
  #chat {
    height: 85%;
    padding: 10px 0px 15px 0px;
    overflow: auto;
    background-color: #f7f8fa;
  }
  .send {
    position: absolute;
    bottom: -10px;
    .s {
      width: 100vw;
      background-color: #f00;
    }
  }
}
</style>

<style>
/*图片气泡*/
.friend_lists {
  margin: 0 50px 0px 10px;
  position: relative;
}

.friend_lists .header_img {
  position: absolute;
  top: 5px;
}
.header_img img {
  width: 40px;
  height: 40px;
}
.me_lists {
  position: relative;
  margin: 0 10px 0px 50px;
}

.me_lists .msg {
  float: right;
  margin-right: 73px;
}

.me_lists .header_img {
  position: absolute;
  top: 5px;
  right: 0px;
}

/*背景气泡*/
.msg-left-white {
  background-color: #ffffff;
  font-size: 16px;
}

.msg-right-green {
  background-color: #008bf3;
  font-size: 16px;
}

.msg-wrapper p {
  color: black;
  display: inline-block;
  padding: 12px;
  margin: 10px;
  position: relative;
  border-radius: 5px;
  text-align: left;
}

.msg-wrapper.right p {
  margin-left: 60px;
}

.msg-wrapper.right p:before {
  width: 0;
  position: absolute;
  top: 15px;
  border-style: solid;
  content: "";
}

.msg-wrapper.right p:before {
  left: -12px;
  border-color: transparent #ffffff;
  border-width: 0 12px 12px 0;
}

.msg-wrapper.left p:before {
  width: 0;
  position: absolute;
  top: 15px;
  border-style: solid;
  content: "";
}

.msg-wrapper.left p {
  margin-right: 60px;
}

.msg-wrapper.left {
  text-align: right;
}

.text-left {
  text-align: left;
}
.me_lists .msg-wrapper p {
  color: #fff;
}

.msg-wrapper.left p:before {
  right: -12px;
  border-color: transparent #008dec;
  border-width: 0 0 12px 12px;
}

.cur_time {
  text-align: center;
  width: 100%;
  margin-top: 10px;
}

.cur_time span {
  width: auto;
  display: inline-block;
  padding: 2px 10px;
  font-size: 14px;
  border-radius: 3px;
  background: #cecece;
  color: white;
}

footer {
  position: fixed;
  width: 100%;
  height: 50px;
  min-height: 50px;
  border-top: solid 1px #bbb;
  left: 0px;
  bottom: 0px;
  overflow: hidden;
  padding: 0px 65px 0 10px;
  background-color: #fafafa;
}

.footer-left {
  position: absolute;
  width: 50px;
  height: 50px;
  left: 0px;
  bottom: 0px;
  text-align: center;
  vertical-align: middle;
  line-height: 100%;
  padding: 12px 4px;
}

.footer-right {
  position: absolute;
  width: 60px;
  height: 50px;
  right: 0px;
  bottom: 0px;
  text-align: center;
  vertical-align: middle;
  line-height: 100%;
  padding: 8px 15px 0 0;
  display: inline-block;
}

.footer-center {
  height: 100%;
  padding: 5px 0px;
}

.footer-center [class*="input"] {
  width: 100%;
  height: 100%;
  border-radius: 5px;
}

.footer-center .input-text {
  background: #fff;
  border: solid 1px #ddd;
  padding: 10px !important;
  font-size: 16px !important;
  line-height: 18px !important;
  font-family: verdana !important;
  overflow: hidden;
}

.footer-center .input-sound {
  background-color: #eee;
}

.mui-content {
  height: 100%;
  padding: 0px 0px 50px 0px;
  overflow: auto;
  background-color: #eaeaea;
}

#msg {
  height: 100%;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
</style>
